package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.constant.ClueStatusConstants;
import com.qk.domain.clue.ClueDO;
import com.qk.domain.clue.CluePoolListDO;
import com.qk.management.aop.anno.LoginAnno;
import com.qk.dto.clue.*;
import com.qk.entity.Clue;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 *
 * @author sch
 * @version 1.0
 * @since 2025-10-13-17:03
 */
@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;


    @LoginAnno
    @PostMapping
    public Result addClue(@RequestBody ClueDTO dto) {
        log.info("添加线索：{}", dto);

        clueService.addClue(dto);
        return Result.success();
    }

    @GetMapping
    public Result list(ClueListDTO dto) {
        log.info("查询线索列表：{}", dto);
        PageResult<ClueDO> result = clueService.clueList(dto);
        return Result.success(result);
    }

    @LoginAnno
    @PutMapping("/assign/{clueId}/{userId}")
    public Result updateById(@PathVariable Integer clueId, @PathVariable Integer userId) {
        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClueInfoById(@PathVariable Integer id) {
        log.info("根据ID查询线索详细信息, id: {}", id);
        ClueVO vo = clueService.getClueById(id);
        return Result.success(vo);
    }

    @LoginAnno
    @PutMapping
    public Result updateClueInfoById(@RequestBody UpdateClueInfoDTO dto) {
        log.info("更新线索信息：{}", dto);
        clueService.updateClueInfoById(dto);
        return Result.success();
    }

    @PutMapping("toBusiness/{id}")
    @LoginAnno
    public Result toBusiness(@PathVariable Integer id) {
        log.info("将指定线索转商机, id: {}", id);
        clueService.toBusiness(id);
        return Result.success();
    }

    //给接口用于将指定线索标记为伪线索
    @PutMapping("/false/{id}")
    @LoginAnno
    public Result toFalse(@PathVariable Integer id, @RequestBody MarkFalseClueDTO dto) {
        log.info("将指定线索标记为伪线索, id: {}, reason: {}, remark: {}", id, dto.getReason(), dto.getRemark());
        clueService.toFalse(id, dto);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result cluePool(CluePoolListDTO dto) {
        log.info("查询线索池列表：{}", dto);
        PageResult<CluePoolListDO> result = clueService.cluePool(dto);
        return Result.success(result);
    }
}
